var a,adr,auth,Delay=50,GM
var mp,dis=[1,1,1,1,1,1,1,1,1,1],b,i,j
var op={weekday:"short",year:"numeric",month:"numeric",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}
var Rel_chg_a,IO_chg_a,Log_chg_a
var tb="<table class=ctrlb cellspacing=7 ondragstart=\"return false;\" ondrop=\"return false;\" border=0 style='font-size:8.5pt;color:#FFF;width:650px;padding-bottom:8px'><tr><td colspan=4 id='phc_l2' style='font-size:11px;color:#903;padding-left:12px'><tr>"
var tb1="<table id='one' cellspacing=7 border=0 ondragstart=\"return false;\" ondrop=\"return false;\" style='background:#bbb;color:#FFF;width:300px'>"
var tbzx="<table id='one-zx' cellspacing=7 border=0 style='background:#eee'>"

var bg_e= "background-color:#059105' class=bt"
var bg_a= "background-color:#5A4848' class=bt"
var bg_he="background-color:#089DF4' class=bt"
var bg_ha="background-color:#033451' class=bt"
var bg_ee="background-color:#837373;border:2px inset #ddd' class=bt1>"
var bg_ea="background-color:#6F9576;border:2px inset #ddd' class=bt1>"
var zxbg_e= "<div class='led led-green' style='display:inline-block'>"
var zxbg_a= "<div class='led led-white' style='display:inline-block'>"
var zx5fld="&nbsp;Rel5-8 schaltet <b style='color:black'>L</b>-Phase"
var zx5fnd="&nbsp;Rel5-8 schaltet <b style='color:blue'>N</b>-Neutral"
var zx1fld="&nbsp;Rel1-4 schaltet <b style='color:black'>L</b>-Phase"
var zx1fnd="&nbsp;Rel1-4 schaltet <b style='color:blue'>N</b>-Neutral"
var zx5fle="&nbsp;Rel5-8 switch <b style='color:black'>L</b>-Phase"
var zx5fne="&nbsp;Rel5-8 switch <b style='color:blue'>N</b>-Neutral"
var zx1fle="&nbsp;Rel1-4 switch <b style='color:black'>L</b>-Phase"
var zx1fne="&nbsp;Rel1-4 switch <b style='color:blue'>N</b>-Neutral"
var zx1nod="&nbsp;Rel1-4 sind ohne Strom."
var zx1noe="&nbsp;Rel1-4 no power supply"
var zx1nodc="&nbsp;Rel1-4 sind ohne Stromversorgung und können nicht geschaltet werden."
var zx1noec="&nbsp;Rel1-4 are without power supply and cannot be switched."


var newP = document.createElement('p')
var ts,tt
var i_div=100
var cos_div=100
f_av =[0,0,0,0,0]
var fp=0, av, nn, wd=[0,0,0,0,0,0,0,0] ,wi=[0,0,0,0,0,0,0,0]
var tea_key
Freq_L1 = new Array()
Freq_L2 = new Array()
Freq_L1_I=0
Freq_L2_I=0

//if(parent.Typ=='z')Delay=200

gI = function () {}
gI.id = function (div) { return document.getElementById(div);}
gN = function () {}
gN.n = function (div) { return document.getElementsByName(div)[0];}

function getOS(){
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;
if (macosPlatforms.indexOf(platform) !== -1) { os = 'Mac OS';}
else if (iosPlatforms.indexOf(platform) !== -1) { os = 'iOS';}
else if (windowsPlatforms.indexOf(platform) !== -1) { os = 'Windows';}
else if (/Android/.test(userAgent)) { os = 'Android';}
else if (!os && /Linux/.test(platform)) {os = 'Linux';}
return os;
}

function rdt(){
tg=new XMLHttpRequest()
tg.open("GET","txt/text_"+parent.Lang+'.txt',false)
tg.send("")
parent.ttd = tg.responseText.split("\r\n")
}

function lg(){
l=new XMLHttpRequest()
l.open("GET","txt/lang_"+parent.Lang+'.txt',false)
l.send("")
parent.lang_txt = l.responseText.split("\r\n")
}

function slg(){
if(parent.Lang==0){tt=",";ts="."}
if(parent.Lang==1){tt=".";ts=","}
var nodes= document.body.getElementsByTagName('*'),
L=nodes.length
while(L){
temp= nodes[--L].id || ''
if(temp.indexOf('lg_')== 0){
var nr=parseInt(temp.substring(3,6))
var tmp=gI.id(temp).innerHTML
gI.id(temp).innerHTML=parent.lang_txt[nr]+tmp}}
}

function slg_s(nr){
return parent.lang_txt[nr]
}

function dds(id){
gI.id(id).innerHTML="<span style='color:#990000'>"+slg_s(60)+"</span>"
gI.id("tb1").style.display="none"
gI.id("tb2").style.display="none"
gI.id("tb3").style.display="none"
}

function split_bin(i){
ret=new Array("","","","","","","","","","","","","","","","") 
var bin=parseInt(i).toString(2)
for(var j=0;j<bin.length;j++)
if(bin.substring(bin.length-1-j,bin.length-j)=="1") ret[j]="1"
return ret
}

function ok_fail_res(){
gI.id("ok").style.display="none"
gI.id("fail").style.display="none"
}

function sc(t,f){
var f=document.forms[f];
for(var i=0;i<f.elements.length;i++){
if(f.elements[i].getAttribute('onchange')==null)
{f.elements[i].onchange=function(){gN.n(t).style.color='#CC0066';gN.n(t).style.fontWeight = "bold"}}}
}

function nf( x, len, sep, ts){
x = Math.round( x * Math.pow(10, len) ) / Math.pow(10, len);
str_no = x+"";
arr_int = str_no.split(".");
if(!arr_int[0]) arr_int[0] = "0";
if(!arr_int[1]) arr_int[1] = "";
if(arr_int[1].length < len){
nk = arr_int[1];
for(i=arr_int[1].length+1; i <= len; i++){  nk += "0";  }    arr_int[1] = nk;}
if(ts != "" && arr_int[0].length > 3){
b = arr_int[0];
arr_int[0] = "";
for(j = 3; j < b.length ; j+=3){
Extrakt = b.slice(b.length - j, b.length - j + 3);
arr_int[0] = ts + Extrakt +  arr_int[0] + "";}
str_first = b.substr(0, (b.length % 3 == 0)?3:(b.length % 3));
arr_int[0] = str_first + arr_int[0];}
return arr_int[0]+sep+arr_int[1];
}

function p_tm(t,typ){
if(typ==0)	// hh:mm
	tim=aLZ( t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)	
if(typ==1)	// hh:mm:ss
	tim=aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==2)	// tt.mm.jjjj, hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"."+aLZ(t.getUTCMonth()+1,2)+"."+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==3)	// tt.mm.jjjj hh-mm-ss
	tim=aLZ(t.getUTCDate(),2)+"-"+aLZ(t.getUTCMonth()+1,2)+"-"+t.getUTCFullYear()+" "+aLZ(t.getUTCHours(),2)+"."+aLZ(t.getUTCMinutes(),2)+"."+aLZ(t.getUTCSeconds(),2)	
if(typ==4)	// WT tt.mm.jjjj, hh:mm:ss
	tim=parent.lang_txt[t.getUTCDay()+255]+" "+aLZ(t.getUTCDate(),2)+"."+aLZ(t.getUTCMonth()+1,2)+"."+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==5)	// tt/mm/jjjj hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear()+" "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)
if(typ==6)	// WT tt/mm/jjjj, hh:mm:ss english
	tim=parent.lang_txt[t.getUTCDay()+255]+" "+aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear()+", "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)	
if(typ==7)	// tt/mm/jj hh:mm:ss
	tim=aLZ(t.getUTCDate(),2)+"/"+aLZ(t.getUTCMonth()+1,2)+"/"+t.getUTCFullYear().toString().substr(-2)+" "+aLZ(t.getUTCHours(),2)+":"+aLZ(t.getUTCMinutes(),2)+":"+aLZ(t.getUTCSeconds(),2)
return tim
}

function aLZ(n,l){
var num = '' + n
while (num.length < l) num = '0' + num
return num
}

function repl(x){
var a=x.toString()
if(parent.Lang==0){
a=a.replace('.','')
a=a.replace(',','.')}
if(parent.Lang==1){
a=a.replace(',','')
a=a.replace('.','.')}
return a
}

function l_0(v){
var nz=16-v.length
if(nz) return Array(nz+1).join("0")+v 
return v
}

function B(b){
return Boolean(Number(b))
}

function init(){
xhttp=new XMLHttpRequest()
xhttp.open("GET","start.cfg",false)
xhttp.send("")
var p = xhttp.responseText.split(";") 

if(p[9]=='end'){
parent.Name=p[0]
newP.innerHTML=p[1]
parent.Name_Func=newP.innerHTML
parent.IP=p[2]
parent.Port=p[3]
var ll=p[4].split(",") 
parent.Login="<b>"+ll[0]+"</b> von "+ll[1]
var pp=p[5].split(",") 
parent.Typ=pp[0]
parent.UTyp=pp[1]
if(pp.length>2)parent.Typ_PRO_Power=pp[2] //8=PRO;''=Power
parent.Ver=p[6]
parent.alw=parseInt(p[7]).toString(2)}
parent.Lang=p[8]
gI.id("lg_13").href+='?'+parent.Lang
parent.build=" --- build: "+p[10]+" - "+p[11]
parent.No_Login = p[12]
}

function bit_test(num, bit){return ((num>>bit) % 2 != 0)}
function bit_set(num, bit){return num | 1<<bit}
function bit_clear(num, bit){return num & ~(1<<bit)}

function ArrayAvg(myArray, No) {
var i = 0, summ = 0, ArrayLen = myArray.length, r=0.0;
while (i < ArrayLen){summ = summ + parseInt(myArray[i++])};
r = parseFloat((summ / ArrayLen)).toFixed(1);
return r
}

function tm(){
try{xhttp=new XMLHttpRequest()
xhttp.open("GET","daten.cfg",false)
xhttp.overrideMimeType('text\/plain; charset=x-user-defined');
xhttp.send("")
var p = xhttp.responseText.split(";")} catch(e){return false}
if(p[0]=='logout'){
document.head.innerHTML="<!doctype html><meta http-equiv='content-type' content='text/html; charset=utf-8'><meta name='viewport' content='width=device-width'><style>body{font-family:Verdana;padding:150px;width:250px;}</style>"
document.title='Net-PwrCtrl Logout'
document.body.innerHTML = "<div style='background-color:#CCC;padding:30px'>NET-PwrCtrl - Logout<br><br>Close the Browser</div></html>"
return}
if(p[11]=='end'){
parent.a_time=p[0]*1000
parent.Rel_chg=p[1]
parent.IO_chg=p[2]
parent.Log_chg=p[3]
parent.ClockStat=p[4]
parent.BootTime=parseInt(p[5])+parseInt(p[6])*65536
parent.Temp=nf(parseFloat(p[7]),1,tt,ts)
parent.Log_o=p[8]
parent.Log_t=p[9]
parent.timer_no=p[10]

if(parent.Typ=='z'){
parent.Fase_L1=p[12]
parent.Fase_L2=p[13]
parent.zx_nol2 = p[24]

Freq_L1[Freq_L1_I]= p[23]
if(Freq_L1.length>5)Freq_L1.shift(); else Freq_L1_I++
Freq_L2[Freq_L2_I]= p[22]
if(Freq_L2.length>5)Freq_L2.shift(); else Freq_L2_I++ 
gI.id("F_L1").innerHTML = p[23]+" Hz"
gI.id("F_L2").innerHTML = p[22]+" Hz"

//gI.id("led_DBUG").innerHTML =  (p[24]/80).toFixed(1) + " A<br>" + p[25]

fase();

for(var i=1;i<9;i++){
parent.Opto_state[i]=p[13+i]
if(parent.Opto_state[i]=="1")gI.id("led_r"+i).className = "led led-white"
else gI.id("led_r"+i).className = "led led-green"}}

if(p[18]=='s') os=6; else os=0
parent.S_ON=p[12+os]
if(p[12+os]=='s'){
parent.Sens_Beg=p[13+os]
if(p[14+os]==parseFloat(128.86)) p[14+os]='---'; else p[14+os]=nf(parseFloat(p[14+os]),2,tt,ts)+' °C'
if(p[15+os]==parseFloat(113.0))  p[15+os]='---'; else p[15+os]=nf(parseFloat(p[15+os]),1,tt,ts)+' %'
parent.S_Temp=p[14+os]
parent.S_Hum=p[15+os]
parent.S_Lux=p[16+os]+' lx'
parent.First_Sens=p[17+os]
parent.Temp_sw=p[18+os]
parent.SHA256_Test=p[19+os]}}
}

function fase(){
if(parent.Fase_L1=="1"){
if(parent.Lang==0)gI.id("ph_l1").innerHTML = zx5fld; else gI.id("ph_l1").innerHTML = zx5fle
gI.id("led_l1").className = "led_s led-white"}
else {
if(parent.Lang==0)gI.id("ph_l1").innerHTML = zx5fnd; else gI.id("ph_l1").innerHTML = zx5fne
gI.id("led_l1").className = "led_s led-blue"}  

if(parent.Fase_L2=="1"){
if(parent.Lang==0)gI.id("ph_l2").innerHTML = zx1fld; else gI.id("ph_l2").innerHTML = zx1fle
gI.id("led_l2").className = "led_s led-white"}
else {
if(parent.Lang==0)gI.id("ph_l2").innerHTML = zx1fnd; else gI.id("ph_l2").innerHTML = zx1fne
gI.id("led_l2").className = "led_s led-blue"}

if(parent.zx_nol2=="L"){
if(parent.Lang==0)gI.id("ph_l2").innerHTML = zx1nod; else gI.id("ph_l2").innerHTML = zx1noe
gI.id("led_l2").className = "led_m led-yellow"}
}

function s(t,adr,auth){
xhttps=new XMLHttpRequest()
xhttps.open("POST",adr+"ctrl.htm"+auth,false)
xhttps.setRequestHeader("Content-type","text/plain")
if(t=='h') xhttps.send('h=0'); else xhttps.send(t)
}

function rel_r(adr,auth){
xhttpe=new XMLHttpRequest()
xhttpe.open("GET",adr+"strg.cfg"+auth,false)
xhttpe.send("")
a = xhttpe.responseText.split(";")
if(parent.Typ=='z'){
for(var i=1;i<9;i++){
if(a[56+i]=="1")top.document.getElementById("led_r"+i).className = "led led-white"
else top.document.getElementById("led_r"+i).className = "led led-green"}}
}

function io_r(adr,auth){
try{xhttpi=new XMLHttpRequest()
xhttpi.open("GET",adr+"ioa.cfg"+auth,false)
xhttpi.send("")
a = xhttpi.responseText.split(";")} catch(f){}
}

function sw_F(i,b){
b.style.boxShadow="2px 2px 2px #fff";	
b.style.textShadow="2px 2px 2px rgba(0,0,0,0.5)"
Rel_chg_a++
s('F'+i+'=S','','')
del_rel()
}

function sw_f(i){
if(wd[i]>0){wd[i]=0
Rel_chg_a++
s('f'+i+'=S','','')
del_rel()}	
}

function sw_g(i){
if(wd[i]==0){wd[i]++
Rel_chg_a++
s('g'+i+'=S','','')
del_rel()}	
}

function sw_E(i,b){
b.style.boxShadow="2px 2px 2px #fff";	
b.style.textShadow="2px 2px 2px rgba(0,0,0,0.5)"
IO_chg_a++
s('E'+i+'=S','','')
del_io()
}

function sw_e(i){
if(wi[i]>0){wi[i]=0
IO_chg_a++
s('e'+i+'=S','','')
del_io()}	
}

function sw_d(i){
if(wi[i]==0){wi[i]++
IO_chg_a++
s('d'+i+'=S','','')
del_io()}
}

function r_read(adr,auth){
var b,c,i
rel_r(adr,auth)
var t='', sf=0, N
if(parent.OS=="Android" || parent.OS=="iOS") t="2>"; else t=">" 
if(parent.ttd===undefined)rdt()
if(a[40]=='end'){
b=tb
for(i=0;i<8;i++){N=a[i*5+4];parent.d_alw[i]=a[i*5+2];if(parent.d_alw[i]==0&&sf==0){parent.tab_nr=i;sf=1}
if(a[i*5+2]==1){b+="<td id=rbt"+i+" title='"+parent.lang_txt[25]+"' style='";if(a[i*5+1]==1)b+=bg_ea; else b+=bg_ee;} //gesperrt
else {
var Nr=a[i*5+3];Nr++
b+="<td id=rbt"+i+" title='"+parent.ttd[Nr]
if(a[i*5+2]==1) b+="'"
else{if(a[i+41]==1){
if(parent.OS=="iOS")b+="' ontouchstart=\";sw_g("+i+")\" onmouseup=\";sw_f("+i+")\""
else if(parent.OS=="Android") b+="' ontouchstart=\";sw_g("+i+")\" ontouchend=\";sw_f("+i+")\""
else b+="' onmousedown=\";sw_g("+i+")\" onmouseup=\";sw_f("+i+")\""}
else b+="' onmousedown=\";sw_F("+i+",this);parent.soc_no="+i+"\""}
b+=" style='";if(N.match('B'))b+='box-shadow: 0 0 0 2px #fff inset;';
if(a[i+41]==1){if(a[i*5+1]==1) b+=bg_he+t; else b+=bg_ha+t} else {if(a[i*5+1]==1) b+=bg_e+t; else b+=bg_a+t}}
b+="<div class=bbt>"+a[i*5]+"</div>"
if(a[i+49]!='0'){b+="<div class=bt_div title=\""
if(N.match('IO'))b+=parent.lang_txt[33]+"\n"
if(N.match('W'))b+=parent.lang_txt[28]+"\n"
if(N.match('K'))b+=parent.lang_txt[26]+"\n"
if(N.match('B'))b+=parent.lang_txt[27]+"\n"
if(N.match('P'))b+=parent.lang_txt[34]+"\n"
if(N.match('S'))b+=parent.lang_txt[18]
b+="\" style=\"font-size:27px;"
if(N.match('W')) b+="color:#1de08f"; else b+="color:#bbb" 
b+="\">&#"+(a[i+49])+";</div>"}
b+="<div class=bt_div>"+N+"</div>"
if(i==3){
b+="<tr height=30>"}}
b+="</table>" 
document.getElementById('rel_z').innerHTML=b}
}

function io_read(adr,auth){
var b,i;a=""
io_r(adr,auth)
var t=''
if(parent.OS=="Android" || parent.OS=="iOS") t="2>"; else t=">" 
if(a.length==1) return
try{document.getElementById('io_div').style.display="inline"}catch(e){}
if(a[33]=='end'){
b=tb	
for(i=0;i<8;i++){
var s=B(a[i*4+2]),ea=B(a[i*4+3]),inv=B(a[i*4+4])
b+="<td title='"
if(!ea){b+=parent.lang_txt[29]
if(inv){b+="\n"+parent.lang_txt[30];s=!s}
if(a[i+34]==1){if(parent.OS=="iOS")b+="' ontouchstart=\";sw_d("+i+")\" onmouseup=\";sw_e("+i+")\""
else if(parent.OS=="Android") b+="' ontouchstart=\";sw_d("+i+")\" ontouchend=\";sw_e("+i+")\""
else b+="' onmousedown=\"sw_d("+i+")\" onmouseup=\"sw_e("+i+")\""}
else b+="' onmousedown=\"sw_E("+i+",this)\""
b+=" style='";if(a[i+50]=='1')b+='box-shadow: 0 0 0 2px #fff inset;';
if(a[i+34]==1){if(s) b+=bg_he+t; else b+=bg_ha+t} else {if(s) b+=bg_e+t; else b+=bg_a+t}}
else{b+=parent.lang_txt[31]
if(inv){b+="\n"+parent.lang_txt[30];s=!s}
b+="' style='"
if(s) b+=bg_ee; else b+=bg_ea}
b+="<div class=bbt>"+a[i*4+1]+"</div>" 
if(a[i+42]!='0')b+="<div class=bt_div style=\"font-size:27px\">&#"+(a[i+42])+";</div>"
if(i==3)b+="<tr height=30>"}
b+="</table>" 
document.getElementById('io_z').innerHTML=b}
}

function del_rel(){window.setTimeout("r_read('','')",Delay)}
function del_io(){window.setTimeout("io_read('','')",Delay)}
function ref(){rel_ch(); if(parent.Typ!='z') io_ch()}
function rel_ch(){if(parent.Rel_chg!=Rel_chg_a){Rel_chg_a=parent.Rel_chg;parent.rel_changed=1;r_read('','')}
if(parent.Typ=='z'){if(parent.zx_nol2=="L"){	
if(parent.Lang==0)document.getElementById('phc_l2').innerHTML = zx1nodc; 
else document.getElementById('phc_l2').innerHTML = zx1noec} else document.getElementById('phc_l2').innerHTML =""}
}
function io_ch(){if(parent.IO_chg!=IO_chg_a){IO_chg_a=parent.IO_chg;io_read('','')}}
